package com.caoustc.okhttplib.okhttp.utils

import com.caoustc.okhttplib.okhttp.ProgressRequestBody
import com.caoustc.okhttplib.okhttp.ProgressRequestBody.OkHttpProgressCallback
import okhttp3.Interceptor
import okhttp3.Response
import java.io.IOException

/**
 * Created by cz on 2016/11/15.
 */
class UpLoadProgressInterceptor(private val progressListener: OkHttpProgressCallback) :
    Interceptor {
    @Throws(IOException::class)
    override fun intercept(chain: Interceptor.Chain): Response {
        val originalRequest = chain.request()
        if (originalRequest.body() == null) {
            return chain.proceed(originalRequest)
        }
        val progressRequest = originalRequest.newBuilder()
            .method(
                originalRequest.method(),
                ProgressRequestBody(originalRequest.body(), progressListener)
            )
            .build()
        return chain.proceed(progressRequest)
    }
}